<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 二级产线数据 -->
      <el-col :span="3" :xs="24">
        <!-- <el-input v-model="lineName" placeholder="请输入产线名称" clearable size="small" prefix-icon="el-icon-search"
          style="margin-bottom: 20px" /> -->
        <el-tree ref="tree" node-key="value" highlight-current :data="lineDate" :props="defaultProps" @node-click="lineClick" />
      </el-col>
      <el-col :span="21" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <!-- <el-form-item label="部门ID" prop="deptId">
            <el-input
              v-model="queryParams.deptId"
              placeholder="请输入部门ID"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          -->
          <el-form-item label="产品" prop="productId">
            <el-select v-model="queryParams.productId" placeholder="请选择产品" clearable >
            <el-option v-for="item in productList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="极限节拍" prop="limitTime">
            <el-input v-model="queryParams.limitTime" placeholder="请输入极限节拍" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="标准节拍" prop="standardTime">
            <el-input v-model="queryParams.standardTime" placeholder="请输入标准节拍" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['zndd:beat_set:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
              v-hasPermi="['zndd:beat_set:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['zndd:beat_set:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['zndd:beat_set:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="beat_setList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" label="序号" />
          <!-- <el-table-column label="主键" align="center" prop="id" /> -->
          <!-- <el-table-column label="部门ID" align="center" prop="deptId" /> -->
          <!-- <el-table-column label="产品ID" align="center" prop="productId" /> -->
          <el-table-column label="产品" align="center" prop="productId" :formatter="productFormatter" />
          <el-table-column label="极限节拍（秒）" align="center" prop="limitTime" />
          <el-table-column label="标准节拍（秒）" align="center" prop="standardTime" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['zndd:beat_set:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['zndd:beat_set:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改产品节拍配置信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <!-- <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item> -->
        <!-- <el-form-item label="产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品ID" />
        </el-form-item> -->
        <!-- <el-form-item label="产线ID" prop="lineId">
          <el-input v-model="form.lineId" placeholder="请输入产品ID" />
        </el-form-item> -->
        <el-form-item label="产品" prop="productId">
          <el-select v-model="form.productId" placeholder="请选择产品">
            <el-option v-for="item in productList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="极限节拍（秒）" prop="limitTime">
          <el-input v-model="form.limitTime" placeholder="请输入极限节拍" />
        </el-form-item>
        <el-form-item label="标准节拍（秒）" prop="standardTime">
          <el-input v-model="form.standardTime" placeholder="请输入标准节拍" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBeat_set, getBeat_set, delBeat_set, addBeat_set, updateBeat_set } from "@/api/zndd/product/beat_set";
import { listDetails} from "@/api/zndd/line/details";
import { listProducttype} from "@/api/zndd/product/producttype";

export default {
  name: "Beat_set",
  data() {
    return {
      //产品数组
      productList:[],
      //二级产线名称
      lineName: null,
      //当前产线ID
      currentLineId:null,
      //二级产线数据
      lineDate: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      //二级产线默认查询条件
      lineParams: {
        pageNum: 1,
        pageSize: 50,
        deptId: null,
        sonLineName: null
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品节拍配置信息表格数据
      beat_setList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        productId: null,
        lineId: null,
        limitTime: null,
        standardTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productId: { required: true, message: '请选择产品', trigger: 'blur' },
        limitTime: { required: true, message: '请输入极限节拍', trigger: 'blur' },
        standardTime: { required: true, message: '请输入标准节拍', trigger: 'blur' }
      }
    };
  },
  watch: {
    // 根据名称筛选产线
    lineName(val) {
      this.lineParams.sonLineName = val;
      this.getLineList();
    }
  },
  created() {
    //获取产品数组
    listProducttype().then(response => {
      var list = [];
        response.rows.forEach(row => {
          list.push({
            label: row.productTypeName,
            value: row.id
          })
        });
        this.productList = list;
    });
    this.getLineList();
  },
  methods: {
    //产品列表格式化
    productFormatter(row, column){
      var lt = null;
      this.productList.forEach(product => {
        if (product.value == row.productId) {
          lt = product;
        }
      });
      if(lt == null){
        return "";
      }else{
        return lt.label;
      }
    },
    //树型二级产线点击事件
    lineClick(data) {
      this.queryParams.lineId = data.value;
      this.currentLineId = data.value;
      this.getList();
    },
    //查询二级产线内容
    getLineList() {
      listDetails(this.lineParams).then(response => {
        var list = [];
        response.rows.forEach(row => {
          list.push({
            label: row.sonLineName,
            value: row.id
          })
        });
        if (list.length > 0) {
          this.queryParams.lineId = list[0].value;
          this.currentLineId = list[0].value;
        }
        this.lineDate = list;
        this.$nextTick(()=>{
          this.$refs.tree.setCurrentKey(this.lineDate[0].value);
        })
        this.getList();
      })
    },
    /** 查询产品节拍配置信息列表 */
    getList() {
      this.loading = true;
      listBeat_set(this.queryParams).then(response => {
        this.beat_setList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        productId: null,
        limitTime: null,
        standardTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品节拍配置信息";
      this.form.lineId = this.currentLineId;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBeat_set(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品节拍配置信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBeat_set(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBeat_set(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的产品节拍配置信息？').then(function () {
        return delBeat_set(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zndd/beat_set/export', {
        ...this.queryParams
      }, `节拍配置信息_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="css" scoped>
::v-deep .el-tree-node__content:hover{
  background-color: #409EFF;
  color: #ffffff;
}
::v-deep .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content{
  background-color: #409EFF;
  color: #ffffff;
}
</style>